import common from "../../../utils/common.js";
const app = getApp()
const ip = app.globalData.ipStr
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const { voiceSvgSrc, maleSvgSrc, femaleSvgSrc } = require('../../../utils/iconSvg')
const defaultColor = '#A6A6A6'
// const defaultColor = 'rgba(153,153,153, 1)';
const activeColor = 'rgba(255, 127, 80)';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 房屋需求数据
        defaultAvatarUrl:defaultAvatarUrl,
        //  1.发布人信息 2.标题 3.内容省略展示  
        needHouseArr: [
            // {viewCount:100, title:'求租，，，，', content:'位置柯翰辰'},
            { id: 123, viewCount: 100, title: '求租，，，，', userInfo: { avatarUrl: defaultAvatarUrl, nickName: '水瓜', publishTime: '15小时前更新' } },
            { id: 123, viewCount: 89, title: '求租333，，，，', userInfo: { avatarUrl: defaultAvatarUrl, nickName: '水瓜2', publishTime: '35分钟前更新' } },
            { id: 123, viewCount: 65, title: '求租90，，，，', userInfo: { avatarUrl: defaultAvatarUrl, nickName: '水瓜3', publishTime: '7天前更新' } },
            { id: 89, viewCount: 6, title: '求租60，，，，', userInfo: { avatarUrl: defaultAvatarUrl, nickName: '水瓜4', publishTime: '12-03更新' } },
        ],
        sort: 'latest',
        voiceSvgSrc: voiceSvgSrc(defaultColor),
        maleSvgSrc: maleSvgSrc(defaultColor), 
        femaleSvgSrc: femaleSvgSrc(defaultColor),

        // 查询结果数组
        needHouseArr: [],

        // 分页参数 搜索时用
        current: 1,
        pages: 1,
        size: 10,
    },

    clickSort(e) {
        var sort = e.currentTarget.dataset.sort;
        this.setData({ sort })
    },

    clickNeedHouse(e){
        var id = e.currentTarget.dataset.id;
        var url = '/pages/nuan/need-info/needInfo?id=' + id;
        common.navigateTo2(url);
    },

    loadNeedData() {
        var tempArr = this.data.needHouseArr;
        var tempCurrent = this.data.current;

        if (tempCurrent > this.data.pages) {
            console.info('tempCurrent=' + tempCurrent + " 已超过总页数=" + this.data.pages);
            return;
        }
        wx.request({
            url: 'http://' + ip + '/house-need/getNeedPage?sort=' + this.data.sort,
            method: 'POST',
            header: {
                'content-type': 'application/json;charset=UTF-8'
            },
            data: { current: tempCurrent, size: this.data.size },
            success: res => { //wx.request成功的回调
                console.info('加载租房需求信息返回信息=' + JSON.stringify(res))
                if (res.statusCode == 200) {
                    if (tempCurrent > 1) {
                        const records = tempArr.concat(res.data.data.records);
                        this.setData({ needHouseArr: records })
                    } else {
                        this.setData({ needHouseArr: res.data.data.records, pages: res.data.data.pages })
                    }
                    this.setData({
                        current: tempCurrent + 1
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (wx.getStorageSync("token")) {
            common.checkUser(ip, '/pages/nuan/user/userInfo');
        } else {
            app.login_1128_2().then(tokenStr => {
                common.checkUser(ip, '/pages/nuan/user/userInfo');
            })
        }
        this.loadNeedData();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})